package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.IntentionMapper;
import com.springboot_gradrecruit.model.dto.intention.AddIntentionRequest;
import com.springboot_gradrecruit.model.dto.intention.PageIntentionRequest;
import com.springboot_gradrecruit.model.dto.intention.UpdateIntentionRequest;
import com.springboot_gradrecruit.model.entity.Intention;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.vo.intention.IntentionVO;
import com.springboot_gradrecruit.service.IntentionService;
import io.swagger.v3.core.util.Json;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zr
 * @description 针对表【intention】的数据库操作Service实现
 * @createDate 2024-12-06 14:49:27
 */
@Service
public class IntentionServiceImpl extends ServiceImpl<IntentionMapper, Intention>
        implements IntentionService {

    @Resource
    private IntentionMapper intentionMapper;

    @Override
    public Long creatIntention(AddIntentionRequest addIntentionRequest, HttpServletRequest request) {
        //1.获取参数
        Intention intention = new Intention();
        intention.setIntentionUserId(addIntentionRequest.getIntentionUserId());
        intention.setIntentionStudentId(addIntentionRequest.getIntentionStudentId());
        intention.setIntentionName(addIntentionRequest.getIntentionName());
        intention.setIntentionContactInfo(addIntentionRequest.getIntentionContactInfo());
        intention.setIntentionHighestEducation(addIntentionRequest.getIntentionHighestEducation());
        intention.setIntentionMajor(addIntentionRequest.getIntentionMajor());
        intention.setIntentionJobTitle(addIntentionRequest.getIntentionJobTitle());
        intention.setIntentionLocation(addIntentionRequest.getIntentionLocation());
        intention.setIntentionExpectedSalary(addIntentionRequest.getIntentionExpectedSalary());
        intention.setIntentionJobType(addIntentionRequest.getIntentionJobType());
        intention.setIntentionAvailableStartDate(addIntentionRequest.getIntentionAvailableStartDate());
        intention.setIntentionStatus(addIntentionRequest.getIntentionStatus());
        intention.setTagsList(addIntentionRequest.getTagsList());
        intention.setJobsList(addIntentionRequest.getJobsList());
        //2.校验是否存在
        QueryWrapper<Intention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("intention_user_id", addIntentionRequest.getIntentionUserId());
        intentionMapper.selectOne(queryWrapper);
        if (intentionMapper.selectOne(queryWrapper) != null) {
            throw new RuntimeException("当前用户已添加意向信息");
        }
        //3.保存
        int insert = intentionMapper.insert(intention);
        return (long) insert;
    }

    @Override
    public boolean updateIntentionById(UpdateIntentionRequest updateIntentionRequest) {
        //1. 获取参数
        Long intentionId = updateIntentionRequest.getIntentionId();
        String intentionName = updateIntentionRequest.getIntentionName();
        String intentionContactInfo = updateIntentionRequest.getIntentionContactInfo();
        String intentionHighestEducation = updateIntentionRequest.getIntentionHighestEducation();
        String intentionMajor = updateIntentionRequest.getIntentionMajor();
        String intentionJobTitle = updateIntentionRequest.getIntentionJobTitle();
        String intentionLocation = updateIntentionRequest.getIntentionLocation();
        String intentionExpectedSalary = updateIntentionRequest.getIntentionExpectedSalary();
        String intentionJobType = updateIntentionRequest.getIntentionJobType();
        Date intentionAvailableStartDate = updateIntentionRequest.getIntentionAvailableStartDate();
        String intentionStatus = updateIntentionRequest.getIntentionStatus();
        String tagsList = updateIntentionRequest.getTagsList();
        String jobsList = updateIntentionRequest.getJobsList();

        //2.更新求职意向信息
        LambdaUpdateWrapper<Intention> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Intention::getIntentionId, intentionId)
                .set(Intention::getIntentionName, intentionName)
                .set(Intention::getIntentionContactInfo, intentionContactInfo)
                .set(Intention::getIntentionHighestEducation, intentionHighestEducation)
                .set(Intention::getIntentionMajor, intentionMajor)
                .set(Intention::getIntentionJobTitle, intentionJobTitle)
                .set(Intention::getIntentionLocation, intentionLocation)
                .set(Intention::getIntentionExpectedSalary, intentionExpectedSalary)
                .set(Intention::getIntentionJobType, intentionJobType)
                .set(Intention::getIntentionAvailableStartDate, intentionAvailableStartDate)
                .set(Intention::getIntentionStatus, intentionStatus)
                .set(Intention::getTagsList, tagsList)
                .set(Intention::getJobsList, jobsList);
        return intentionMapper.update(null, updateWrapper)>0;
    }

    @Override
    public PageResponse<IntentionVO> pageIntention(PageIntentionRequest pageIntentionRequest) {
        //1.获取参数
        String intentionHighestEducation = pageIntentionRequest.getIntentionHighestEducation();
        String intentionLocation = pageIntentionRequest.getIntentionLocation();
        String intentionJobType = pageIntentionRequest.getIntentionJobType();

        //2.构建分页条件
        Page<Intention> page = pageIntentionRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Intention> intentionPage = lambdaQuery()
                .like(StrUtil.isNotBlank(intentionHighestEducation), Intention::getIntentionHighestEducation, intentionHighestEducation)
                .like(StrUtil.isNotBlank(intentionLocation), Intention::getIntentionLocation, intentionLocation)
                .like(StrUtil.isNotBlank(intentionJobType), Intention::getIntentionJobType, intentionJobType)
                .page(page);

        return PageResponse.of(intentionPage, IntentionVO.class);

    }

    @Override
    public List<IntentionVO> queryIntentionByJobTitle(List<String> jobTitle) {
        List<IntentionVO> intentionVOS = new ArrayList<>();
        for (String title : jobTitle) {
            QueryWrapper<Intention> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("intention_status","公开");
            List<Intention> intentions = intentionMapper.selectList(queryWrapper);
            intentions.forEach(intention -> {
                String jobsList = intention.getJobsList();
                //json格式处理
                Gson gson = new Gson();
                String[] jobArray = gson.fromJson(jobsList, String[].class);
                for (String job : jobArray) {
                    if (title.contains(job)) {
                        IntentionVO intentionVO = BeanUtil.copyProperties(intention, IntentionVO.class);
                        intentionVOS.add(intentionVO);
                    }
                }
            });
        }
        // 去重操作
        List<IntentionVO> distinctList = new ArrayList<>(intentionVOS.stream()
                .collect(Collectors.toMap(IntentionVO::getIntentionId, vo -> vo, (existing, replacement) -> existing))
                .values());
        return distinctList;
    }

    @Override
    public List<IntentionVO> queryIntentionByJobIndustry(List<String> jobIndustry) {
        List<IntentionVO> intentionVOS = new ArrayList<>();
        for (String industry : jobIndustry) {
            QueryWrapper<Intention> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("intention_status","公开");
            List<Intention> intentions = intentionMapper.selectList(queryWrapper);
            intentions.forEach(intention -> {
                String jobsList = intention.getTagsList();
                //json格式处理
                Gson gson = new Gson();
                String[] jobArray = gson.fromJson(jobsList, String[].class);
                for (String job : jobArray) {
                    if (industry.contains(job)) {
                        IntentionVO intentionVO = BeanUtil.copyProperties(intention, IntentionVO.class);
                        intentionVOS.add(intentionVO);
                    }
                }
            });
        }

        // 去重操作
        List<IntentionVO> distinctList = new ArrayList<>(intentionVOS.stream()
                .collect(Collectors.toMap(IntentionVO::getIntentionId, vo -> vo, (existing, replacement) -> existing))
                .values());
        return distinctList;
    }
}




